from app01 import models
from django.core.validators import RegexValidator
from django.core.exceptions import ValidationError
from django import forms
from app01.utils.bootstrap import BootStrapModelForm
from app01.utils.encrypt import md5


# 部门
class DepartmentModelForm(BootStrapModelForm):
    class Meta:
        model = models.Department
        fields = "__all__"

    def clean_title(self):
        title = self.cleaned_data.get("title")
        # 去数据库校验当前输入数据是否已经存在于数据库
        exists = models.Department.objects.exclude(id=self.instance.pk).filter(title=title).exists()
        if exists:
            raise ValidationError("该部门已存在")
        return title


# 用户
class UserInfoModelForm(BootStrapModelForm):
    class Meta:
        model = models.UserInfo
        fields = "__all__"
        widgets = {
            "password": forms.PasswordInput(render_value=True)
        }

    def clean_sid(self):
        # 当前编辑的哪一行的ID
        # print(self.instance.pk)
        txt_sid = self.cleaned_data["sid"]
        exists = models.UserInfo.objects.exclude(id=self.instance.pk).filter(sid=txt_sid).exists()
        if exists:
            raise ValidationError("该学号已存在")
        # 验证通过，用户输入的值返回
        return txt_sid


# 管理员
class AdminModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True)
    )

    class Meta:
        model = models.Admin
        fields = ["username", 'password', "confirm_password", "level"]
        widgets = {
            "password": forms.PasswordInput(render_value=True)
        }

    def clean_password(self):
        pwd = self.cleaned_data.get("password")
        return md5(pwd)

    def clean_confirm_password(self):
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))
        if confirm != pwd:
            raise ValidationError("密码不一致")
        # 返回什么，此字段以后保存到数据库就是什么。
        return confirm

    def clean_username(self):
        username = self.cleaned_data.get("username")
        # 去数据库校验当前输入数据是否已经存在于数据库
        exists = models.Admin.objects.exclude(id=self.instance.pk).filter(username=username).exists()
        if exists:
            raise ValidationError("该管理员已存在")
        return username


class AdminEditModelForm(BootStrapModelForm):
    class Meta:
        model = models.Admin
        fields = ['username']

    def clean_username(self):
        username = self.cleaned_data.get("username")
        # 去数据库校验当前输入数据是否已经存在于数据库
        exists = models.Admin.objects.exclude(id=self.instance.pk).filter(username=username).exists()
        if exists:
            raise ValidationError("该管理员已存在")
        return username


class AdminResetModelForm(BootStrapModelForm):
    confirm_password = forms.CharField(
        label="确认密码",
        widget=forms.PasswordInput(render_value=True)
    )

    class Meta:
        model = models.Admin
        fields = ['password', 'confirm_password']
        widgets = {
            "password": forms.PasswordInput(render_value=True)
        }

    def clean_password(self):
        # pwd = self.password 直接获取未经表单验证的字段值
        pwd = self.cleaned_data.get("password")  # 从表单验证后的数据中获取字段值
        md5_pwd = md5(pwd)

        # 去数据库校验当前密码和新输入的密码是否一致
        exists = models.Admin.objects.filter(id=self.instance.pk, password=md5_pwd).exists()
        if exists:
            raise ValidationError("不能与以前的密码相同")

        return md5_pwd

    def clean_confirm_password(self):
        pwd = self.cleaned_data.get("password")
        confirm = md5(self.cleaned_data.get("confirm_password"))

        if pwd is None:
            raise ValidationError("不能与以前的密码相同")

        if confirm != pwd:
            raise ValidationError("密码不一致")
        # 返回什么，此字段以后保存到数据库就是什么。
        return confirm


class AdminLevelModelForm(BootStrapModelForm):
    class Meta:
        model = models.Admin
        fields = ['level']


# 签到
class RecordModelForm(BootStrapModelForm):
    class Meta:
        model = models.Record
        fields = "__all__"
